﻿/* 
 * Godtear.com
 * QQ:bzure@189.cn
 *Author:Nathan Liu
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bzure.Security.Permissions
{
    [Serializable]
    public abstract class DroitCollection
    {
        protected IDictionary<string, IDroit> _droitList;
        public IEnumerable<IDroit> DriotCollection
        {
            get { return _droitList.Values; }
        }
        public IDroit this[string key]
        {
            get { return _droitList.ContainsKey(key) ? _droitList[key] : null; }
        }
        public DroitCollection()
        {
            _droitList = new Dictionary<string, IDroit>();
        }
        public virtual void AddDroit(IDroit droit)
        {
            IDroit drt = _droitList.ContainsKey(droit.Guid.ToString()) ? _droitList[droit.Guid.ToString()] : null;
            bool find = false;
            if (drt != null)
            {
                foreach (IAccessCode accessType in droit.AccessCodes)
                {
                    find = false;
                    foreach (IAccessCode at in drt.AccessCodes)
                        if (at.CodeValue == accessType.CodeValue)
                        {
                            find = true;
                            break;
                        }
                    if (!find)
                        drt.AccessCodes.Add(accessType.Copy ());
                }
            }
            else
                _droitList.Add(droit.Guid.ToString(), droit.Copy ());
        }
        public virtual void RemoveDroit(IDroit droit)
        {
            IDroit drt = _droitList[droit.Guid.ToString()];
            if (drt != null)
            {
                foreach (IAccessCode accessType in droit.AccessCodes)
                {
                    foreach (IAccessCode at in drt.AccessCodes)
                        if (at.CodeValue == accessType.CodeValue)
                        {
                            drt.AccessCodes.Remove(at);
                            break;
                        }
                }
            }
        }
        public virtual void AddDroit(IEnumerable<IDroit> droits)
        {
            foreach (IDroit droit in droits)
                this.AddDroit(droit);
        }
        public virtual void ClearDroit()
        {
            _droitList.Clear();
        }
    }
}
